package cn.always.xiajia.admin.oauth2.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import cn.always.xiajia.admin.oauth2.entity.OAuth2Client;
import cn.always.xiajia.admin.oauth2.mapper.OAuth2ClientMapper;
import cn.always.xiajia.admin.oauth2.service.OAuth2ClientService;
import cn.always.xiajia.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.always.xiajia.framework.common.exception.util.ServiceExceptionUtil;
import cn.always.xiajia.framework.mybatis.core.service.impl.BaseServiceImplX;

/**
 * 
 * @author xgj
 *
 */
@Service("oAuth2ClientService")
public class OAuth2ClientServiceImpl extends BaseServiceImplX<OAuth2ClientMapper, OAuth2Client> implements OAuth2ClientService {

	/**
	 * 校验OAuth2客服端
	 * 
	 * @param reqDTO
	 * @return
	 */
	@Override
	public OAuth2Client validOAuthClientFromCache(String clientId) {
		Map<String, Object> param = new HashMap<>();
		param.put("EQ_clientId", clientId);
		OAuth2Client oAuth2Client = xjGetOne(param);
		if (oAuth2Client == null) {
			// OAuth2 客户端不存在
			throw ServiceExceptionUtil.exception(GlobalErrorCodeConstants.ADMIN_000001);
		}

		if (!"0".equals(oAuth2Client.getStatus())) {
			// OAuth2 客户端已禁用
			throw ServiceExceptionUtil.exception(GlobalErrorCodeConstants.ADMIN_000003);
		}

		return oAuth2Client;
	}

}
